<template>
  <picker mode="selector" :range="options" :range-key="labelKey" @change="handleChange">
    <div class="form-item">
      <div class="label">
        {{ label }}
      </div>

      <div class="content">
        <div v-if="!selectedItem" class="placeholder">
          {{ placeholder }}
        </div>
        <div v-else>
          {{ selectedItem[labelKey] }}
        </div>
        <arrow-right />
      </div>
    </div>
  </picker>
</template>

<script>
import ArrowRight from './cpns/arrow-right.vue'

export default {
  name: 'ZePicker',
  components: { ArrowRight },
  props: {
    label: { type: String, default: '' },
    placeholder: { type: String, default: '请选择' },
    options: { type: Array, default: () => [] },
    labelKey: { type: String, default: 'label' },
    valueKey: { type: String, default: 'value' },
    value: {},
  },
  data() {
    return {
      selectedItem: null,
    }
  },
  methods: {
    handleChange(e) {
      this.selectedItem = this.options[e.detail.value]
      this.$emit('input', this.selectedItem[this.valueKey])
      this.$emit('change', this.selectedItem)
    },
  },
}
</script>

<style lang="scss" scoped>
.form-item {
  font-size: 14px;
  font-weight: 400;
  line-height: 14px;
  padding: 15px 0;
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid #F2F2F2;
  .label {
    color: #333333;
  }

  .content {
    display: flex;
    align-items: center;
    column-gap: 5px;
  }

  .placeholder {
    color: #999999;
  }
}
</style>
